from Common.conftest import GetToken
from Common.request_method import RestClient
from Common.logger import logger
import requests


class StorageAllocation(GetToken, RestClient):

    def __init__(self):
        global base_url
        base_url = "https://www.test.carvingerp.com"

    def ad_group_configs(self, get_token, get_api_profiler):
        """
        SB获取广告组配置
        :param get_token:
        :param get_api_profiler:
        :return:
        """
        url = base_url + "/python/v1/ad_sb_policy/ad_group_configs"
        headers = {
            "Authorization": get_token,
            "Carving-Advertising-Api-Profile": get_api_profiler
        }
        params = {"module": "sb_phrase_targeting_keyword_normal"}
        response = self.send_requests(method="get", url=url, headers=headers,params=params)
        return response

    def ad_policy_broad_search_check(self, get_token, get_api_profiler):
        """
        SP广泛搜索词检测
        :param get_token:
        :param get_api_profiler:
        :return:
        """
        url = base_url + "/python/v1/ad_policy/broad_search_policy"
        headers = {
            "Authorization": get_token,
            "Carving-Advertising-Api-Profile": get_api_profiler
        }
        data = {
            "ad_type": "sp",
            "rule_model": 1,
            "module": "sp_broad_search_keyword_model_1",
            "campaign_configs": [
                {
                    "campaign_id": 122356139507810,
                    "campaign_name": "US-17*33in 电暖垫-手动-B0B3MBQC57",
                    "hight_down_limit": 2,
                    "hight_up_limit": 3.5,
                    "low_down_limit": 0.2,
                    "price": 39.99,
                    "target_cr": 20
                }
            ],
            "ad_group_configs": [
                {
                    "ad_campaign_type": "sp_manual_campaign",
                    "ad_group_id": 26500014051094,
                    "ad_group_name": "广泛组",
                    "ad_group_type": "sp_broad_keyword_group",
                    "campaign_id": 122356139507810,
                    "campaign_name": "US-17*33in 电暖垫-手动-B0B3MBQC57",
                    "id": 805,
                    "portfolio_id": 22396183415978,
                    "portfolio_name": "加热垫",
                    "sys_sp_group_id": 805
                }
            ]
        }
        response = self.send_requests(method="post", url=url, headers=headers, json=data)
        return response

    def ad_sb_policy_phrase_search_policy(self, get_token, get_api_profiler):
        """
        SB词组搜索策略
        :param get_token:
        :param get_api_profiler:
        :return:
        """
        url = base_url + "/python/v1/ad_sb_policy/phrase_search_policy"
        headers = {
            "Authorization": get_token,
            "Carving-Advertising-Api-Profile": get_api_profiler
        }
        json = {
            "ad_type": "sb",
            "rule_model": "normal",
            "module": "sb_phrase_keyword_normal",
            "campaign_configs": [
                {
                    "campaign_id": 311381433627248,
                    "campaign_name": "US-康虹电暖垫2.0-视频短语组-B0C8T5ZHMK",
                    "hight_up_limit": 3.5,
                    "low_down_limit": 0.2,
                    "price": 39.9,
                    "target_acos": 33,
                    "target_cr": 23.63
                }
            ],
            "ad_group_configs": [
                {
                    "ad_campaign_type": "sb_manual_campaign",
                    "ad_group_id": 514887225308081,
                    "ad_group_name": "视频短语",
                    "ad_group_type": "sb_phrase_keyword_group",
                    "campaign_id": 311381433627248,
                    "campaign_name": "US-康虹电暖垫2.0-视频短语组-B0C8T5ZHMK",
                    "id": 30,
                    "portfolio_id": 23637579501260,
                    "portfolio_name": "加热垫2.0",
                    "sys_sb_group_id": 30
                }
            ]
        }
        response = self.send_requests(method="post", url=url, headers=headers, json=json)
        return response

    def ad_sb_policy_phrase_targeting_policy(self, get_token, get_api_profiler):
        """
        SB词组投放策略
        :param get_token:
        :param get_api_profiler:
        :return:
        """
        url = base_url + "/python/v1/ad_sb_policy/phrase_targeting_policy"
        headers = {
            "Authorization": get_token,
            "Carving-Advertising-Api-Profile": get_api_profiler
        }
        json = {
            "ad_type": "sb",
            "rule_model": "normal",
            "module": "sb_phrase_targeting_keyword_normal",
            "ad_group_configs": [
                {
                    "ad_campaign_type": "sb_manual_campaign",
                    "ad_group_id": 97735426060509,
                    "ad_group_name": "AdGroup1",
                    "ad_group_type": "sb_phrase_keyword_group",
                    "campaign_id": 106102251791361,
                    "campaign_name": "US-17*33in 电暖垫-视频短语-B0B3MBQC57",
                    "id": 21,
                    "portfolio_id": 22396183415978,
                    "portfolio_name": "加热垫",
                    "sys_sb_group_id": 21
                }
            ],
            "campaign_configs": [
                {
                    "campaign_id": 106102251791361,
                    "campaign_name": "US-17*33in 电暖垫-视频短语-B0B3MBQC57",
                    "hight_up_limit": 3.5,
                    "low_down_limit": 0.6,
                    "price": 28.22,
                    "target_acos": 33,
                    "target_cr": 20
                }
            ]
        }
        response = self.send_requests(method="post", url=url, headers=headers, json=json)
        return response
